package com.stateflow.engine.user.biz.impl;

import com.stateflow.engine.core.cache.UserAllDataCache;
import com.stateflow.engine.core.service.StateFlowCoreService;
import com.stateflow.engine.user.biz.service.StateFlowService;
import com.stateflow.engine.util.SignCheckUtils;
import com.stateflow.facade.common.model.req.CommonRequest;
import com.stateflow.facade.common.model.res.CommonResult;
import com.stateflow.facade.common.template.CommonTemplate;
import com.stateflow.facade.common.util.CommonAssert;
import com.stateflow.facade.dto.OperatorDTO;
import com.stateflow.facade.dto.StateFlowConfigDTO;
import com.stateflow.facade.dto.req.GetOperatorListRequestDTO;
import com.stateflow.facade.dto.req.StateFlowRequestDTO;
import com.stateflow.facade.dto.notify.StateFlowNotifyDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by weiqingming on 2019/12/10.
 * 状态流转处理业务层
 */
@Service("stateFlowService")
public class StateFlowServiceImpl implements StateFlowService{

    private static final Logger LOGGER = LoggerFactory.getLogger(StateFlowServiceImpl.class);


    @Resource(name = "stateFlowCoreService")
    private StateFlowCoreService stateFlowCoreService;


    @Override
    public CommonResult stateFlow(CommonRequest request) {
        return CommonTemplate.execute(request, LOGGER, new CommonTemplate() {
            @Override
            protected void check() {
                StateFlowRequestDTO req = commonCheck(request, StateFlowRequestDTO.class);
                CommonAssert.isNoBlankStr(req.getAccessKey(),"accessKey必传");
                CommonAssert.isNoBlankStr(req.getConfigId(),"configId必传");
                CommonAssert.isNoBlankStr(req.getCurrentState(),"currentState必传");
                CommonAssert.isNoBlankStr(req.getOperatorCode(),"operatorCode必传");
                CommonAssert.isNoBlankStr(req.getOrderNumber(),"orderNumber必传");
                CommonAssert.isNoBlankStr(req.getSign(),"sign必传");
                SignCheckUtils.checkUserSign(req, req.getOrderNumber(), req.getOperatorCode());
            }

            @Override
            protected CommonResult business() {
                StateFlowRequestDTO req = getParam();

                // 获取状态流转配置
                StateFlowConfigDTO config = UserAllDataCache.getConfig(req.getAccessKey(),req.getConfigId());

                // 交给核心层处理
                StateFlowNotifyDTO result = stateFlowCoreService.stateFlow(req, config);
                return new CommonResult(result);
            }
        });
    }

    @Override
    public CommonResult getOperatorList(CommonRequest request) {
        return CommonTemplate.execute(request, LOGGER, new CommonTemplate() {
            @Override
            protected void check() {
                GetOperatorListRequestDTO req = commonCheck(request, GetOperatorListRequestDTO.class);
                CommonAssert.isNoBlankStr(req.getAccessKey(),"accessKey必传");
                CommonAssert.isNoBlankStr(req.getConfigId(),"configId必传");
                CommonAssert.isNoBlankStr(req.getState(),"state状态必传");
                CommonAssert.isNoBlankStr(req.getSign(),"sign必传");
                SignCheckUtils.checkUserSign(req, req.getConfigId(), req.getState());
            }

            @Override
            protected CommonResult business() {

                GetOperatorListRequestDTO req = getParam();

                // 获取状态流转配置
                StateFlowConfigDTO config = UserAllDataCache.getConfig(req.getAccessKey(), req.getConfigId());

                // 调用核心层获取数据
                List<OperatorDTO> result = stateFlowCoreService.listOperator(req.getState(), config);
                return new CommonResult(result);
            }
        });
    }




}
